home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Python-1.4 / Lib / uu.py < prev    next >
Text File  |  1998-06-24  |  5KB  |  170 lines

  1. # uu.py
  2. # Copyright 1994 by Lance Ellinghouse
  3. # Cathedral City, California Republic, United States of America.
  4. #                        All Rights Reserved
  5. # Permission to use, copy, modify, and distribute this software and its 
  6. # documentation for any purpose and without fee is hereby granted, 
  7. # provided that the above copyright notice appear in all copies and that
  8. # both that copyright notice and this permission notice appear in 
  9. # supporting documentation, and that the name of Lance Ellinghouse
  10. # not be used in advertising or publicity pertaining to distribution 
  11. # of the software without specific, written prior permission.
  12. # LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
  13. # THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  14. # FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE CENTRUM BE LIABLE
  15. # FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  18. # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. #
  20. # Modified by Jack Jansen, CWI, July 1995:
  21. # - Use binascii module to do the actual line-by-line conversion
  22. #   between ascii and binary. This results in a 1000-fold speedup. The C
  23. #   version is still 5 times faster, though.
  24. # - Arguments more compliant with python standard
  25. #
  26. # This file implements the UUencode and UUdecode functions.
  27.  
  28. # encode(in_file, out_file [,name, mode])
  29. # decode(in_file [, out_file, mode])
  30.  
  31. import binascii
  32. import os
  33. import string
  34.  
  35. Error = 'uu.Error'
  36.  
  37. def encode(in_file, out_file, name=None, mode=None):
  38.     """Uuencode file"""
  39.     #
  40.     # If in_file is a pathname open it and change defaults
  41.     #
  42.     if in_file == '-':
  43.     in_file = sys.stdin
  44.     elif type(in_file) == type(''):
  45.     if name == None:
  46.         name = os.path.basename(in_file)
  47.     if mode == None:
  48.         try:
  49.         mode = os.stat(in_file)[0]
  50.         except AttributeError:
  51.         pass
  52.     in_file = open(in_file, 'rb')
  53.     #
  54.     # Open out_file if it is a pathname
  55.     #
  56.     if out_file == '-':
  57.     out_file = sys.stdout
  58.     elif type(out_file) == type(''):
  59.     out_file = open(out_file, 'w')
  60.     #
  61.     # Set defaults for name and mode
  62.     #
  63.     if name == None:
  64.     name = '-'
  65.     if mode == None:
  66.     mode = 0666
  67.     #
  68.     # Write the data
  69.     #
  70.     out_file.write('begin %o %s\n' % ((mode&0777),name))
  71.     str = in_file.read(45)
  72.     while len(str) > 0:
  73.     out_file.write(binascii.b2a_uu(str))
  74.     str = in_file.read(45)
  75.     out_file.write(' \nend\n')
  76.  
  77.  
  78. def decode(in_file, out_file=None, mode=None):
  79.     """Decode uuencoded file"""
  80.     #
  81.     # Open the input file, if needed.
  82.     #
  83.     if in_file == '-':
  84.     in_file = sys.stdin
  85.     elif type(in_file) == type(''):
  86.     in_file = open(in_file)
  87.     #
  88.     # Read the header line, and fill in optional args if needed
  89.     #
  90.     hdr = in_file.readline()
  91.     if not hdr:
  92.     raise Error, 'Empty input file'
  93.     hdrfields = string.split(hdr)
  94.     if len(hdrfields) <> 3 or hdrfields[0] <> 'begin':
  95.     raise Error, ('Incorrect uu header line', hdr)
  96.     if out_file == None:
  97.     out_file = hdrfields[2]
  98.     if mode == None:
  99.     mode = string.atoi(hdrfields[1])
  100.     #
  101.     # Open the output file
  102.     #
  103.     if out_file == '-':
  104.     out_file = sys.stdout
  105.     elif type(out_file) == type(''):
  106.     fp = open(out_file, 'wb')
  107.     try:
  108.         os.path.chmod(out_file, mode)
  109.     except AttributeError:
  110.         pass
  111.     out_file = fp
  112.     #
  113.     # Main decoding loop
  114.     #
  115.     str = in_file.readline()
  116.     while str and str != 'end\n':
  117.     out_file.write(binascii.a2b_uu(str))
  118.     str = in_file.readline()
  119.     if not str:
  120.     raise Error, 'Truncated input file'
  121.  
  122. def test():
  123.     """uuencode/uudecode main program"""
  124.     import sys
  125.     import getopt
  126.  
  127.     dopt = 0
  128.     topt = 0
  129.     input = sys.stdin
  130.     output = sys.stdout
  131.     ok = 1
  132.     try:
  133.     optlist, args = getopt.getopt(sys.argv[1:], 'dt')
  134.     except getopt.error:
  135.     ok = 0
  136.     if not ok or len(args) > 2:
  137.     print 'Usage:', sys.argv[0], '[-d] [-t] [input [output]]'
  138.     print ' -d: Decode (in stead of encode)'
  139.     print ' -t: data is text, encoded format unix-compatible text'
  140.     sys.exit(1)
  141.     
  142.     for o, a in optlist:
  143.     if o == '-d': dopt = 1
  144.     if o == '-t': topt = 1
  145.  
  146.     if len(args) > 0:
  147.     input = args[0]
  148.     if len(args) > 1:
  149.     output = args[1]
  150.  
  151.     if dopt:
  152.     if topt:
  153.         if type(output) == type(''):
  154.         output = open(output, 'w')
  155.         else:
  156.         print sys.argv[0], ': cannot do -t to stdout'
  157.         sys.exit(1)
  158.     decode(input, output)
  159.     else:
  160.     if topt:
  161.         if type(input) == type(''):
  162.         input = open(input, 'r')
  163.         else:
  164.         print sys.argv[0], ': cannot do -t from stdin'
  165.         sys.exit(1)
  166.     encode(input, output)
  167.  
  168. if __name__ == '__main__':
  169.     test()
  170.